<?php
namespace App\Api\Controllers\RolePermission;


use App\Api\Controllers\BaseController;
use App\Models\Merchant;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;

class RoleController extends BaseController
{

    //角色列表
    public function role_list(Request $request)
    {
        try {
            $public = $this->parseToken();
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
                $created_id = $public->merchant_id;
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
                $created_id = $public->user_id;

            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table('roles');

            } else {
                $obj = DB::table('roles');

            }
            $roles = $obj->where('created_id', $created_id)
                ->where('guard_name', $guard_name)
                ->select('id as role_id', 'display_name')
                ->get();
            $this->status = 1;
            $this->message = '数据返回成功';
	    
            return $this->format($roles);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    //代理商的权限列表
    public function permission_list(Request $request)
    {
        try {
            $created_id = "";
            $guard_name = '';
            $public = $this->parseToken();
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
                $created_id = $public->merchant_id;
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
                $created_id = $public->user_id;
            }

            $model_has_roles_where = [];
            //服务商以上
            if ($public->level > 0) {
                $model_has_roles_where[] = ['model_id', '=', $created_id];
            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table('model_has_roles');
            } else {
                $obj = DB::table('model_has_roles');
            }

            $model_has_roles = $obj
                ->where('model_type', 'App\Models\User')
                ->where($model_has_roles_where)
                ->select('role_id')
                ->get();
            if ($model_has_roles->isEmpty()) {
                return json_encode([
                    "status" => 2,
                    "message" => "权限还没有分配请联系管理员-1"
                ]);
            }

            $model_has_roles_arr = $model_has_roles->map(function ($value) {
                return (array)$value;
            })->toArray();

            if (env('DB_D1_HOST')) {
                $obj1 = DB::connection("mysql_d1")->table('role_has_permissions');
            } else {
                $obj1 = DB::table('role_has_permissions');
            }

            $role_has_permissions = $obj1
                ->whereIn('role_id', $model_has_roles_arr)
                ->select('permission_id')
                ->get();
            if ($role_has_permissions->isEmpty()) {
                return json_encode([
                    "status" => 2,
                    "message" => "权限还没有分配请联系管理员-2"
                ]);
            }

            $role_has_permissions_arr = $role_has_permissions->map(function ($value) {
                return (array)$value;
            })->toArray();

            if (env('DB_D1_HOST')) {
                $obj2 = DB::connection("mysql_d1")->table('permissions');
            } else {
                $obj2 = DB::table('permissions');
            }

            $list = $obj2->where('pid', 0)
                ->where('guard_name', $guard_name)
                ->select('id as permission_id', 'name', 'pid', 'display_name')
                ->get();
            $list = $list->map(function ($value) {
                return (array)$value;
            })->toArray();
            if ($list) {
                foreach ($list as &$per) {
                    if (env('DB_D1_HOST')) {
                        $obj3 = DB::connection("mysql_d1")->table('permissions');
                    } else {
                        $obj3 = DB::table('permissions');
                    }

                    $child = $obj3->where('pid', $per['permission_id'])
                        ->where('guard_name', $guard_name)
//                        ->whereIn('id', $role_has_permissions_arr)
                        ->select('id as permission_id', 'name', 'pid', 'display_name')
                        ->get();
                    if ($child) $per['child'] = $child;
                }
                unset($per);
            }

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($list);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    //商户的所有权限列表
    public function merchant_permission_list(Request $request)
    {
        try {
            $created_id = "";
            $guard_name = "";
            $public = $this->parseToken();
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
                $created_id = $public->merchant_id;
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
                $created_id = $public->user_id;

            }

            if (env('DB_D1_HOST')) {
                $obj2 = DB::connection("mysql_d1")->table('permissions');

            } else {
                $obj2 = DB::table('permissions');

            }
            $list = $obj2->where('guard_name', $guard_name)
                ->where('pid', '>', '0')
                ->select('id as permission_id', 'name', 'pid', 'display_name')
                ->get();

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($list);


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    //添加角色
    public function add_role(Request $request)
    {
        try {
            $public = $this->parseToken();
            $display_name = $request->get('display_name');
            $pid = $request->get('pid', 0);
            $guard_name = '';
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
                $created_id = $public->merchant_id;
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
                $created_id = $public->user_id;

            }


            $check_data = [
                'display_name' => '角色名字',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $role = Role::create([
                    'name' => time(),
                    'pid' => $pid,
                    'display_name' => $display_name,
                    'guard_name' => $guard_name,
                    'created_id' => $created_id,
                ]
            );


            return json_encode([
                "status" => 1,
                "message" => "添加成功"
            ]);

        } catch (\Exception $e) {
            return json_encode([
                "status" => 2,
                "message" => $e->getMessage()
            ]);
        }
    }


    //添加权限
    public function add_permission(Request $request)
    {
        try {
            $public = $this->parseToken();
            $display_name = $request->get('display_name');
            $pid = $request->get('pid', 0);

            $guard_name = '';
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
            }

            //权限暂不可以添加
            if ($public->level > 0) {
                return json_encode([
                    "status" => 2,
                    "message" => "权限暂不可以添加"
                ]);
            }

            $check_data = [
                'display_name' => '权限名字',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $permission = Permission::create(
                [
                    'name' => $display_name,
                    'display_name' => $display_name,
                    'guard_name' => $guard_name,
                    'pid' => $pid
                ]
            );

            if ($permission) {
                return json_encode([
                    "status" => 1,
                    "message" => "添加成功"
                ]);
            } else {
                return json_encode([
                    "status" => '2',
                    "message" => "添加失败"
                ]);
            }
        } catch (\Exception $ex) {
            return json_encode([
                "status" => '-1',
                "message" => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ]);
        }
    }


    //分配角色
    public function assign_role(Request $request)
    {

        try {
            $public = $this->parseToken();
            $role_id = $request->get('role_id');
            $role_id = explode(',', $role_id);
            $user_id = $request->get('customer_id');
            $guard_name = '';
            $model_type = "";


            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
                $model_type = "App\Models\Merchant";
                $user = Merchant::where('id', $user_id)->first();
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
                $model_type = "App\Models\User";
                $user = User::where('id', $user_id)->first();
                if ($user->pid > 0) {
                    if ($user_id==$public->user_id){
                        return json_encode([
                            'status' => 2,
                            'message' => '自己无法分配自己的权限'
                        ]);
                    }
                }
            }

            if (!$user) {
                return json_encode([
                    'status' => 2,
                    'message' => '用户不存在'
                ]);
            }

            $check_data = [
                'customer_id' => '用户',
                'role_id' => '角色',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }


            DB::table('model_has_roles')
                ->where('model_id', $user_id)
                ->where('model_type', $model_type)
                ->delete();


            foreach ($role_id as $k => $v) {
                $user->assignRole($v);
            }


            return json_encode([
                "status" => 1,
                "message" => "分配成功"
            ]);

        } catch (\Exception $e) {
            return json_encode([
                "status" => 2,
                "message" => $e->getMessage()
            ]);
        }

    }


    //给角色分配权限
    public function assign_permission(Request $request)
    {
        try {
            $public = $this->parseToken();
            $permission_id = $request->get('permission_id');
            $permission_id = explode(',', $permission_id);
            $role_id = $request->get('role_id');
            $role = Role::where('id', $role_id)->first();
            if (!$role) {
                return json_encode([
                    'status' => 2,
                    'message' => '角色不存在'
                ]);
            }

            $assign_permission = Cache::get('assign_permission');
            if ($assign_permission) {
                return json_encode([
                    'status' => 2,
                    'message' => '操作频繁请稍后再试！'
                ]);
            } else {
                Cache::put('assign_permission', '1', 1);
            }

            $guard_name = '';
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
            }

            $check_data = [
                'permission_id' => '权限',
                'role_id' => '角色',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            //先删除
            DB::table('role_has_permissions')
                ->where('role_id', $role_id)
                ->delete();

            foreach ($permission_id as $k => $v) {
                $permission = Permission::where('id', $v)
                    ->select('id')
                    ->first();
                if (!$permission) {
                    continue;
                }
                $role->givePermissionTo($v);
            }


            return json_encode([
                "status" => 1,
                "message" => "分配成功"
            ]);

        } catch (\Exception $e) {
            return json_encode([
                "status" => 2,
                "message" => $e->getMessage()
            ]);
        }
    }


    //查询 用户已分配角色
    public function user_role_list(Request $request)
    {
        try {
            $public = $this->parseToken();
            $user_id = $request->get('customer_id');
            $guard_name = "";
            $model_type = "";
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
                $model_type = "App\Models\Merchant";
                //  $user = Merchant::where('id', $user_id)->first();
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
                $model_type = "App\Models\User";
                // $user = User::where('id', $user_id)->first();

            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table('model_has_roles');
            } else {
                $obj = DB::table('model_has_roles');
            }
	    
            $roles = $obj
                ->where('model_type', $model_type)
                ->where('model_id', $user_id)
                ->select('role_id')
                ->get();

            $this->message = '数据返回成功';
            return $this->format($roles);


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    //查询 角色已分配权限
    public function role_permission_list(Request $request)
    {
        try {
            $public = $this->parseToken();
            $role_id = $request->get('role_id');

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table('role_has_permissions');
            } else {
                $obj = DB::table('role_has_permissions');
            }
	    
            $roles = $obj
                ->where('role_id', $role_id)
                ->select('permission_id')
                ->get();

            $this->message = '数据返回成功';
            return $this->format($roles);


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    //查询 用户已分配权限
    public function user_permission_list(Request $request)
    {
        try {
            $public = $this->parseToken();
            $user_id = $request->get('customer_id');
            $model_type = "";
	    
            if ($public->type == "merchant") {
                $model_type = "App\Models\Merchant";
            }

            if ($public->type == "user") {
                $model_type = "App\Models\User";
            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table('model_has_permissions');
            } else {
                $obj = DB::table('model_has_permissions');
            }
	    
            $roles = $obj
                ->where('model_type', $model_type)
                ->where('model_id', $user_id)
                ->get();

            $this->message = '数据返回成功';
	    
            return $this->format($roles);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //给用户分配权限
    public function user_assign_permission(Request $request)
    {
        try {
            $public = $this->parseToken();
            $permission_id = $request->get('permission_id');
            $permission_id = explode(',', $permission_id);
            $customer_id = $request->get('customer_id');

            $assign_permission = Cache::get('assign_permission');
            if (0&&$assign_permission) {
                return json_encode([
                    'status' => 2,
                    'message' => '操作频繁请稍后再试！'
                ]);
            } else {
                Cache::put('assign_permission', '1', 1);
            }
	    
            $model_type = "";
            $user = "";
            if ($public->type == "merchant") {
                $model_type = "App\Models\Merchant";
                $user = Merchant::where('id', $customer_id)->first();
            }

            if ($public->type == "user") {
                $model_type = "App\Models\User";
                $user = User::where('id', $customer_id)->first();
            }
            $check_data = [
                'permission_id' => '权限',
                'customer_id' => '用户ID',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
	    
            //先删除
            DB::table('model_has_permissions')
                ->where('model_type', $model_type)
                ->where('model_id', $customer_id)
                ->delete();

            foreach ($permission_id as $k => $v) {
                $permission = Permission::where('id', $v)
                    ->select('id')
                    ->first();
                if (!$permission) {
                    continue;
                }
                $user->givePermissionTo($v);
            }

            return json_encode([
                "status" => 1,
                "message" => "分配成功"
            ]);
        } catch (\Exception $e) {
            return json_encode([
                "status" => 2,
                "message" => $e->getMessage()
            ]);
        }
    }


    public function del_role(Request $request)
    {
        try {
            $public = $this->parseToken();
            $role_id = $request->get('role_id');
            $guard_name = '';
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
            }
	    
            Role::where('id', $role_id)->delete();
            $this->message = '角色删除成功';
	    
            return $this->format();
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    public function del_permission(Request $request)
    {
        try {
            $public = $this->parseToken();
            $role_id = $request->get('permission_id');
            $guard_name = '';
	    
            if ($public->type == "merchant") {
                $guard_name = 'merchant.api';
            }

            if ($public->type == "user") {
                $guard_name = 'user.api';
            }

            //权限暂不可以添加
            if ($public->level > 0) {
                return json_encode([
                    "status" => 2,
                    "message" => "权限暂不可以删除"
                ]);
            }

            Permission::orWhere('id', $role_id)
                ->orWhere('pid', $role_id)
                ->delete();
            $this->message = '权限删除成功';
	    
            return $this->format();
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


}
